<?php

namespace app\admin\model\goods;

use think\Model;


class GoodsDiscount extends Model
{

    

    

    // 表名
    protected $name = 'goods_discount';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'discount_type_text',
        'condition_type_text',
        'apply_type_text',
        'start_time_text',
        'end_time_text',
        'limit_type_text'
    ];
    

    
    public function getDiscountTypeList()
    {
        return ['1' => __('Discount_type 1'), '2' => __('Discount_type 2'), '3' => __('Discount_type 3'), '4' => __('Discount_type 4')];
    }

    public function getConditionTypeList()
    {
        return ['1' => __('Condition_type 1'), '2' => __('Condition_type 2'), '3' => __('Condition_type 3')];
    }

    public function getApplyTypeList()
    {
        return ['1' => __('Apply_type 1'), '2' => __('Apply_type 2'), '3' => __('Apply_type 3'), '4' => __('Apply_type 4'), '5' => __('Apply_type 5')];
    }

    public function getLimitTypeList()
    {
        return ['1' => __('Limit_type 1'), '2' => __('Limit_type 2'), '3' => __('Limit_type 3')];
    }


    public function getDiscountTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['discount_type'] ?? '');
        $list = $this->getDiscountTypeList();
        return $list[$value] ?? '';
    }


    public function getConditionTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['condition_type'] ?? '');
        $list = $this->getConditionTypeList();
        return $list[$value] ?? '';
    }


    public function getApplyTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['apply_type'] ?? '');
        $list = $this->getApplyTypeList();
        return $list[$value] ?? '';
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['start_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['end_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getLimitTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['limit_type'] ?? '');
        $list = $this->getLimitTypeList();
        return $list[$value] ?? '';
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
